﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grinko.Lists
{
    public class MyList : IList
    {
        #region Constructors

        /// <summary>
        /// Empty list constructor
        /// </summary>
        public MyList() : this(new int[0]) { }

        /// <summary>
        /// Parametrized list constructor
        /// </summary>
        /// <param name="x">Array for list inisialisation</param>
        public MyList(int[] x)
        {
            l = new List<int>();
            for (int i = 0; i < x.Count(); i++)
            {
                l.Add(x[i]);
            }
        }

        #endregion

        #region IList implementation

        /// <summary>
        /// Clear list
        /// </summary>
        public void Clear()
        {
            l = null;
        }

        /// <summary>
        /// Add element to the end
        /// </summary>
        /// <param name="value">Element value</param>
        public void Add(int value)
        {
            l.Add(value);
        }

        /// <summary>
        /// Get list item by index
        /// </summary>
        /// <param name="index">Index value</param>
        /// <returns>Item value</returns>
        /// <exception cref="IndexOutOfRangeException">If index out of array range</exception>
        public int GetItem(int index)
        {
            if (index >= Count || index < 0)
                throw new IndexOutOfRangeException("Index is out of list range!");

            return l[index];
        }

        /// <summary>
        /// Amount of elements in array
        /// </summary>
        public int Count
        {
            get
            {
                return l.Count;
            }
        }

        /// <summary>
        /// Arrays compare
        /// </summary>
        /// <param name="comp">array to compare</param>
        /// <returns>result of compare</returns>
        public bool Compare(int[] comp)
        {
            if (comp == null)
                throw new ArgumentNullException();

            if (comp.Length != this.Count)
                return false;

            for (int i = 0; i < this.Count; i++)
            {
                if (comp[i] != GetItem(i))
                    return false;
            }

            return true;
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// list
        /// </summary>
        private List<int> l;

        #endregion
    }
}
